package ctgu.yao.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ctgu.yao.cloud.dto.ApplicationVO;
import ctgu.yao.cloud.dto.PageDTO;
import ctgu.yao.cloud.form.SearchForm2;
import ctgu.yao.cloud.mapper.RecruitmentMapper;
import ctgu.yao.cloud.mapper.ResumeMapper;
import ctgu.yao.cloud.po.Application;
import ctgu.yao.cloud.mapper.ApplicationMapper;
import ctgu.yao.cloud.po.Recruitment;
import ctgu.yao.cloud.po.Resume;
import ctgu.yao.cloud.service.ApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private RecruitmentMapper recruitmentMapper;
 /*   @Override
    public List<ApplicationVO> selectList() {
        return applicationMapper.selectList();
    }*/



    @Override
    public PageDTO selectList(Integer page, Integer size) {
        Page<Application> applicationPage=new Page<>(page,size);
        Page<Application> resultPage = this.applicationMapper.selectPage(applicationPage,null);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        List<ApplicationVO> applicationVOList=new ArrayList<>();
        for(Application application:resultPage.getRecords()){
            ApplicationVO applicationVO=new ApplicationVO();
            BeanUtils.copyProperties(application,applicationVO);
            Recruitment recruitment = this.recruitmentMapper.selectById(application.getReid());
            Resume resume = this.resumeMapper.selectById(application.getRid());
            applicationVO.setName(resume.getName());
            applicationVO.setPhone(resume.getPhone());
            applicationVO.setEmail(resume.getEmail());
            applicationVO.setReName(recruitment.getName());
            applicationVO.setContactName(recruitment.getContactName());
            applicationVO.setContactPhone(recruitment.getContactPhone());
            String state=application.getState();
            if (state.equals("0")) {
                state = "等待通知";
            } else if (state.equals("1")) {
                state = "已接受";
            } else {
                state = "已拒绝";

            }
            applicationVO.setState(state);
            applicationVOList.add(applicationVO);
        }

        pageDTO.setData(applicationVOList);
        return pageDTO;
    }

    @Override
    public PageDTO search(SearchForm2 searchForm2) {
        Page<Application> applicationPage=new Page<>(searchForm2.getPage(),searchForm2.getSize());
        Page<Application> resultPage=null;
        if(searchForm2.getValue().equals("")){
            resultPage=this.applicationMapper.selectPage(applicationPage,null);
        }else{
            QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
            if(searchForm2.getKey().equals("reName")){
                QueryWrapper<Recruitment> recruitmentQueryWrapper=new QueryWrapper<>();
                recruitmentQueryWrapper.like("name",searchForm2.getValue());
                List<Recruitment> recruitments=this.recruitmentMapper.selectList(recruitmentQueryWrapper);
                List<Integer> reidList=new ArrayList<>();
                for(Recruitment recruitment:recruitments){
                    reidList.add(recruitment.getReid());
                }
                queryWrapper.in("reid",reidList);
            }
            if(searchForm2.getKey().equals("name")){
                QueryWrapper<Resume> resumeQueryWrapper=new QueryWrapper<>();
                resumeQueryWrapper.like("name",searchForm2.getValue());
                List<Resume> resumes=this.resumeMapper.selectList(resumeQueryWrapper);
                List<Integer> ridList=new ArrayList<>();
                for(Resume resume:resumes){
                    ridList.add(resume.getRid());
                }
                queryWrapper.in("rid",ridList);
            }
            resultPage=this.applicationMapper.selectPage(applicationPage,queryWrapper);
        }
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        List<ApplicationVO> applicationVOList=new ArrayList<>();
        for(Application application:resultPage.getRecords()){
            ApplicationVO applicationVO=new ApplicationVO();
            BeanUtils.copyProperties(application,applicationVO);
            Recruitment recruitment = this.recruitmentMapper.selectById(application.getReid());
            Resume resume = this.resumeMapper.selectById(application.getRid());
            applicationVO.setName(resume.getName());
            applicationVO.setPhone(resume.getPhone());
            applicationVO.setEmail(resume.getEmail());
            applicationVO.setReName(recruitment.getName());
            applicationVO.setContactName(recruitment.getContactName());
            applicationVO.setContactPhone(recruitment.getContactPhone());
            String state=application.getState();
            if (state.equals("0")) {
                state = "等待通知";
            } else if (state.equals("1")) {
                state = "已接受";
            } else {
                state = "已拒绝";

            }
            applicationVO.setState(state);
            applicationVOList.add(applicationVO);
        }

        pageDTO.setData(applicationVOList);
        return pageDTO;
    }

    @Override
    public PageDTO search1(SearchForm2 searchForm2,Integer cuid) {
        Page<Application> applicationPage=new Page<>(searchForm2.getPage(),searchForm2.getSize());
        Page<Application> resultPage=null;
        QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Recruitment> recruitmentQueryWrapper=new QueryWrapper<>();
        recruitmentQueryWrapper.like("cuid",cuid);
        List<Recruitment> recruitments=this.recruitmentMapper.selectList(recruitmentQueryWrapper);
        List<Integer> reidList=new ArrayList<>();
        for(Recruitment recruitment:recruitments){
            reidList.add(recruitment.getReid());
        }
        queryWrapper.in("reid",reidList);
        if(searchForm2.getValue().equals("")){
            resultPage=this.applicationMapper.selectPage(applicationPage,queryWrapper);
        }else{
//            QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
            if(searchForm2.getKey().equals("reName")){
                QueryWrapper<Recruitment> recruitmentQueryWrapper2=new QueryWrapper<>();
                recruitmentQueryWrapper2.like("name",searchForm2.getValue());
                List<Recruitment> recruitments2=this.recruitmentMapper.selectList(recruitmentQueryWrapper2);
                List<Integer> reidList2=new ArrayList<>();
                for(Recruitment recruitment:recruitments2){
                    reidList2.add(recruitment.getReid());
                }
                queryWrapper.in("reid",reidList2);
            }
            if(searchForm2.getKey().equals("name")){
                QueryWrapper<Resume> resumeQueryWrapper=new QueryWrapper<>();
                resumeQueryWrapper.like("name",searchForm2.getValue());
                List<Resume> resumes=this.resumeMapper.selectList(resumeQueryWrapper);
                List<Integer> ridList=new ArrayList<>();
                for(Resume resume:resumes){
                    ridList.add(resume.getRid());
                }
                queryWrapper.in("rid",ridList);
            }
            resultPage=this.applicationMapper.selectPage(applicationPage,queryWrapper);
        }
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        List<ApplicationVO> applicationVOList=new ArrayList<>();
        for(Application application:resultPage.getRecords()){
            ApplicationVO applicationVO=new ApplicationVO();
            BeanUtils.copyProperties(application,applicationVO);
            Recruitment recruitment = this.recruitmentMapper.selectById(application.getReid());
            Resume resume = this.resumeMapper.selectById(application.getRid());
            applicationVO.setName(resume.getName());
            applicationVO.setPhone(resume.getPhone());
            applicationVO.setEmail(resume.getEmail());
            applicationVO.setReName(recruitment.getName());
            applicationVO.setContactName(recruitment.getContactName());
            applicationVO.setContactPhone(recruitment.getContactPhone());
            String state=application.getState();
            if (state.equals("0")) {
                state = "等待通知";
            } else if (state.equals("1")) {
                state = "已接受";
            } else {
                state = "已拒绝";

            }
            applicationVO.setState(state);
            applicationVOList.add(applicationVO);
        }

        pageDTO.setData(applicationVOList);
        return pageDTO;
    }

    @Override
    public PageDTO search2(SearchForm2 searchForm2,Integer rid) {
        System.out.println(rid);
        Page<Application> applicationPage=new Page<>(searchForm2.getPage(),searchForm2.getSize());
        Page<Application> resultPage=null;
        QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Resume> resumeQueryWrapper=new QueryWrapper<>();
        resumeQueryWrapper.eq("rid",rid);
        List<Resume> resumes=this.resumeMapper.selectList(resumeQueryWrapper);
        List<Integer> ridList=new ArrayList<>();
        for(Resume resume:resumes){
            ridList.add(resume.getRid());
        }
        queryWrapper.in("rid",ridList);
        if(searchForm2.getValue().equals("")){
            resultPage=this.applicationMapper.selectPage(applicationPage,queryWrapper);
        }else{
//            QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
            if(searchForm2.getKey().equals("reName")){
                QueryWrapper<Recruitment> recruitmentQueryWrapper=new QueryWrapper<>();
                recruitmentQueryWrapper.like("name",searchForm2.getValue());
                List<Recruitment> recruitments=this.recruitmentMapper.selectList(recruitmentQueryWrapper);
                List<Integer> reidList=new ArrayList<>();
                for(Recruitment recruitment:recruitments){
                    reidList.add(recruitment.getReid());
                }
                queryWrapper.in("reid",reidList);
            }
            if(searchForm2.getKey().equals("name")){
                QueryWrapper<Resume> resumeQueryWrapper2=new QueryWrapper<>();
                resumeQueryWrapper.like("name",searchForm2.getValue());
                List<Resume> resumes2=this.resumeMapper.selectList(resumeQueryWrapper);
                List<Integer> ridList2=new ArrayList<>();
                for(Resume resume:resumes2){
                    ridList2.add(resume.getRid());
                }
                queryWrapper.in("rid",ridList2);
            }
            resultPage=this.applicationMapper.selectPage(applicationPage,queryWrapper);
        }
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        List<ApplicationVO> applicationVOList=new ArrayList<>();
        for(Application application:resultPage.getRecords()){
            ApplicationVO applicationVO=new ApplicationVO();
            BeanUtils.copyProperties(application,applicationVO);
            Recruitment recruitment = this.recruitmentMapper.selectById(application.getReid());
            Resume resume = this.resumeMapper.selectById(application.getRid());
            applicationVO.setName(resume.getName());
            applicationVO.setPhone(resume.getPhone());
            applicationVO.setEmail(resume.getEmail());
            applicationVO.setReName(recruitment.getName());
            applicationVO.setContactName(recruitment.getContactName());
            applicationVO.setContactPhone(recruitment.getContactPhone());
            String state=application.getState();
            if (state.equals("0")) {
                state = "等待通知";
            } else if (state.equals("1")) {
                state = "已接受";
            } else {
                state = "已拒绝";

            }
            applicationVO.setState(state);
            applicationVOList.add(applicationVO);
        }

        pageDTO.setData(applicationVOList);
        return pageDTO;
    }

    @Override
    public PageDTO selectList2(Integer page, Integer size, Integer cuid) {
        Page<Application> applicationPage=new Page<>(page,size);
        QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Recruitment> recruitmentQueryWrapper=new QueryWrapper<>();
        recruitmentQueryWrapper.like("cuid",cuid);
        List<Recruitment> recruitments=this.recruitmentMapper.selectList(recruitmentQueryWrapper);
        List<Integer> reidList=new ArrayList<>();
        for(Recruitment recruitment:recruitments){
            reidList.add(recruitment.getReid());
        }
        queryWrapper.in("reid",reidList);
        Page<Application> resultPage = this.applicationMapper.selectPage(applicationPage,queryWrapper);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        List<ApplicationVO> applicationVOList=new ArrayList<>();
        System.out.println(cuid);
        for(Application application:resultPage.getRecords()){
            ApplicationVO applicationVO=new ApplicationVO();
            BeanUtils.copyProperties(application,applicationVO);
            Recruitment recruitment = this.recruitmentMapper.selectById(application.getReid());
            Resume resume = this.resumeMapper.selectById(application.getRid());
            applicationVO.setName(resume.getName());
            applicationVO.setPhone(resume.getPhone());
            applicationVO.setEmail(resume.getEmail());
            applicationVO.setReName(recruitment.getName());
            applicationVO.setContactName(recruitment.getContactName());
            applicationVO.setContactPhone(recruitment.getContactPhone());
            String state=application.getState();
            if (state.equals("0")) {
                state = "等待通知";
            } else if (state.equals("1")) {
                state = "已接受";
            } else {
                state = "已拒绝";

            }
            applicationVO.setState(state);
            applicationVOList.add(applicationVO);
        }

        pageDTO.setData(applicationVOList);
        return pageDTO;
    }

    @Override
    public PageDTO selectList3(Integer page, Integer size, Integer rid) {
        Page<Application> applicationPage=new Page<>(page,size);
        QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Resume> resumeQueryWrapper=new QueryWrapper<>();
        resumeQueryWrapper.like("rid",rid);
        List<Resume> resumes=this.resumeMapper.selectList(resumeQueryWrapper);
        List<Integer> ridList=new ArrayList<>();
        for(Resume resume:resumes){
            ridList.add(resume.getRid());
        }
        queryWrapper.in("rid",ridList);
        Page<Application> resultPage = this.applicationMapper.selectPage(applicationPage,queryWrapper);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        List<ApplicationVO> applicationVOList=new ArrayList<>();
        for(Application application:resultPage.getRecords()){
            ApplicationVO applicationVO=new ApplicationVO();
            BeanUtils.copyProperties(application,applicationVO);
            Recruitment recruitment = this.recruitmentMapper.selectById(application.getReid());
            Resume resume = this.resumeMapper.selectById(application.getRid());
            applicationVO.setName(resume.getName());
            applicationVO.setPhone(resume.getPhone());
            applicationVO.setEmail(resume.getEmail());
            applicationVO.setReName(recruitment.getName());
            applicationVO.setContactName(recruitment.getContactName());
            applicationVO.setContactPhone(recruitment.getContactPhone());
            String state=application.getState();
            if (state.equals("0")) {
                state = "等待通知";
            } else if (state.equals("1")) {
                state = "已接受";
            } else {
                state = "已拒绝";

            }
            applicationVO.setState(state);
            applicationVOList.add(applicationVO);
        }

        pageDTO.setData(applicationVOList);
        return pageDTO;
    }

    @Override
    public Boolean update(Application application,Integer rid,Integer reid) {
        System.out.println(rid+reid);
        QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rid",rid).eq("reid",reid);
        Application application1 = this.applicationMapper.selectOne(queryWrapper);
        if(application1!=null) {
            application.setState("1");
            this.applicationMapper.update(application,queryWrapper);
            return true;
        }
        return false;
    }

    @Override
    public Boolean disagree(Integer rid,Integer reid) {
        System.out.println(rid+reid);
        QueryWrapper<Application> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rid",rid).eq("reid",reid);
        Application application1 = this.applicationMapper.selectOne(queryWrapper);
        if(application1!=null) {
            application1.setState("2");
            this.applicationMapper.update(application1,queryWrapper);
            return true;
        }
        return false;
    }

}
